/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	liuyang 
 * 
 * <p>描   述  :   RecheckManager.java for com.pengtu.service.business    </p>
 * 
 * <p>最后修改 : $: 2011-10-10-下午5:33:44 v 1.0.0	 liuyang   $     </p>
 * 
 */

package com.pengtu.service.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.business.CheckPetitionDao;
import com.pengtu.dao.business.CheckReportDao;
import com.pengtu.dao.business.FlowRecheckDao;
import com.pengtu.entity.app.User;
import com.pengtu.entity.business.recheck.CheckPetition;
import com.pengtu.entity.business.recheck.CheckReport;
import com.pengtu.entity.business.recheck.FlowRecheck;
import com.pengtu.entity.history.HistoryProject;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.service.BaseManager;
import com.pengtu.service.history.HistoryProjectManager;

/**
 * 
 * RecheckManager
 * 
 * 2011-10-10 下午5:33:44
 * 
 * @version 1.0.0
 * 
 */
@Component
@Transactional
public class RecheckManager extends BaseManager {
	private FlowRecheckDao flowRecheckDao;
	private CheckPetitionDao checkPetitionDao;
	private CheckReportDao checkReportDao;
	private HistoryProjectManager historyProjectManager;
	
	@Autowired
	public void setHistoryProjectManager(HistoryProjectManager historyProjectManager) {
		this.historyProjectManager = historyProjectManager;
	}

	/**
	 * @param flowRecheckDao
	 *            the flowRecheckDao to set
	 */
	@Autowired
	public void setFlowRecheckDao(FlowRecheckDao flowRecheckDao) {
		this.flowRecheckDao = flowRecheckDao;
	}

	/**
	 * @param checkPetitionDao
	 *            the checkPetitionDao to set
	 */
	@Autowired
	public void setCheckPetitionDao(CheckPetitionDao checkPetitionDao) {
		this.checkPetitionDao = checkPetitionDao;
	}

	/**
	 * @param checkReportDao
	 *            the checkReportDao to set
	 */
	@Autowired
	public void setCheckReportDao(CheckReportDao checkReportDao) {
		this.checkReportDao = checkReportDao;
	}

	/**
	 * 保存 save: 适用:
	 * 
	 * @param flowRecheck
	 * @exception
	 * @since 1.0.0
	 */
	public void save(FlowRecheck flowRecheck) {
		flowRecheckDao.save(flowRecheck);
	}

	public void save(CheckPetition checkPetition) {
		checkPetitionDao.save(checkPetition);
	}

	public void save(CheckReport checkReport) {
		checkReportDao.save(checkReport);
	}

	public void save(FlowRecheck flowRecheck, CheckPetition checkPetition) {
		flowRecheckDao.save(flowRecheck);
		checkPetition.setFlowRecheck(flowRecheck);
		checkPetitionDao.save(checkPetition);
	}

	public void save(FlowRecheck flowRecheck,
			HistoryProject historyProject) {
		flowRecheckDao.save(flowRecheck);
		historyProject.setKey(flowRecheck.getId());
		historyProjectManager.save(historyProject);
	}
	
	/**
	 * 查询 getFlowRecheck: 适用:
	 * 
	 * @param id
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public FlowRecheck getFlowRecheck(String id) {
		return flowRecheckDao.get(id);
	}

	public CheckPetition getCheckPetition(String id) {
		return checkPetitionDao.get(id);
	}

	public CheckReport getCheckReport(String id) {
		return checkReportDao.get(id);
	}

	public Page<FlowRecheck> search(final Page<FlowRecheck> page,
			final QueryFilter queryfilter) {
		return flowRecheckDao.findPage(page, queryfilter);
	}
	
	public CheckPetition findCheckPetition(FlowRecheck flowRecheck){
		return checkPetitionDao.findUniqueBy("flowRecheck", flowRecheck);
	}
	
	public CheckReport findCheckReport(FlowRecheck flowRecheck){
		return checkReportDao.findUniqueBy("flowRecheck", flowRecheck);
	}
	
	/**
	 * 待办任务
	 * findPersonalTasks:
	 * 适用:流程处理
	 * @param user
	 * @param projectInfo
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public List<FlowRecheck> findPersonalTasks(User user, ProjectInfo projectInfo) {
		List<Task> taskList = super.findPersonalTasks(user);
		List<FlowRecheck> flowRechecks = new ArrayList<FlowRecheck>();
		for (Task task : taskList) {
			String taskId = task.getId();
			String exectionId = task.getExecutionId();
			String arg = exectionId.substring(0, 7);
			if ("recheck".equals(arg)) {
				String id = (String) super.getVariableByTaskId(taskId,
						Constants.RECHECK_ID);
				FlowRecheck flowRecheck = flowRecheckDao.get(id);
				if (flowRecheck != null) {
					if (StringUtils.equals(flowRecheck.getProjectInfo()
							.getId(), projectInfo.getId())) {
						flowRecheck.setTaskId(taskId);
						flowRechecks.add(flowRecheck);
					}
				}
			}
		}
		return flowRechecks;
	}
	
	public void takeTask(String taskId, String key, User user,
			HistoryProject historyProject) {
		super.takeTask(taskId, user);
		historyProject.setKey(key);
		historyProjectManager.save(historyProject);
	}
	
	public void completeTask(FlowRecheck flowRecheck, String outcome,
			Map<String, Object> variables, HistoryProject historyProject) {
		super.completeTask(flowRecheck.getTaskId(), outcome, variables);
		historyProjectManager.save(historyProject);
		flowRecheckDao.save(flowRecheck);
	}
}
